package utils;

public class RegexValidator {
    /**
     * 正则验证是否是邮箱
     *
     * @param text
     * @return true:是邮箱
     */
    public static boolean validateEmail(String text) {
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        return text.matches(regex);
    }

    /**
     * 正则验证是否是手机号
     *
     * @param text
     * @return true:是手机号
     */
    public static boolean validateMobile(String text) {
        String[] regexs = new String[]{
                "^(\\+?213|0)(5|6|7)\\d{8}$",
                "^(!?(\\+?963)|0)?9\\d{8}$",
                "^(!?(\\+?966)|0)?5\\d{8}$",
                "^(\\+?1)?[2-9]\\d{2}[2-9](?!11)\\d{6}$",
                "^(\\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$",
                "^(\\+?49[ \\.\\-])?([\\(]{1}[0-9]{1,6}[\\)])?([0-9 \\.\\-\\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$",
                "^(\\+?45)?(\\d{8})$",
                "^(\\+?30)?(69\\d{8})$",
                "^(\\+?61|0)4\\d{8}$",
                "^(\\+?44|0)7\\d{9}$",
                "^(\\+?852\\-?)?[569]\\d{3}\\-?\\d{4}$",
                "^(\\+?91|0)?[789]\\d{9}$",
                "^(\\+?64|0)2\\d{7,9}$",
                "^(\\+?27|0)\\d{9}$",
                "^(\\+?26)?09[567]\\d{7}$",
                "^(\\+?34)?(6\\d{1}|7[1234])\\d{7}$",
                "^(\\+?358|0)\\s?(4(0|1|2|4|5)?|50)\\s?(\\d\\s?){4,8}\\d$",
                "^(\\+?33|0)[67]\\d{8}$",
                "^(\\+972|0)([23489]|5[0248]|77)[1-9]\\d{6}",
                "^(\\+?36)(20|30|70)\\d{7}$",
                "^(\\+?39)?\\s?3\\d{2} ?\\d{6,7}$",
                "^(\\+?81|0)\\d{1,4}[ \\-]?\\d{1,4}[ \\-]?\\d{4}$",
                "^(\\+?6?01){1}(([145]{1}(\\-|\\s)?\\d{7,8})|([236789]{1}(\\s|\\-)?\\d{7}))$",
                "^(\\+?47)?[49]\\d{7}$",
                "^(\\+?32|0)4?\\d{8}$",
                "^(\\+?47)?[49]\\d{7}$",
                "^(\\+?48)? ?[5-8]\\d ?\\d{3} ?\\d{2} ?\\d{2}$",
                "^(\\+?55|0)\\-?[1-9]{2}\\-?[2-9]{1}\\d{3,4}\\-?\\d{4}$",
                "^(\\+?351)?9[1236]\\d{7}$",
                "^(\\+?7|8)?9\\d{9}$",
                "^(\\+3816|06)[- \\d]{5,9}$",
                "^(\\+?90|0)?5\\d{9}$",
                "^(\\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$",
                "^(\\+?0?86\\-?)?1[345789]\\d{9}$",
                "^(\\+?886\\-?|0)?9\\d{8}$"
        };
        for (String regex : regexs) {
            if (text.matches(regex)) {
                return true;
            }
        }
        return false;
    }


    public static String regexValidator(String text) {
        if (validateMobile(text)) {
            return "PhoneNumber";
        } else if (validateEmail(text)) {
            return "Email";
        } else {
            return text;
        }
    }

    public static void main(String[] args) {
        System.out.println(validateMobile("415-579-1638"));
    }
}
